package com.work.crops.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.work.crops.dao.CropsDiseaseMapper;
import com.work.crops.dao.CropsInfoDiseaseMapper;
import com.work.crops.pojo.CropsDisease;
import com.work.crops.pojo.CropsInfoDisease;
import com.work.crops.pojo.CropsParam;
import com.work.crops.service.CropsDiseaseService;
import com.work.entity.Result;
import com.work.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CropsDiseaseServiceImpl implements CropsDiseaseService {

    @Autowired
    private CropsDiseaseMapper cropsDiseaseMapper;

    @Autowired
    private CropsInfoDiseaseMapper cropsInfoDiseaseMapper;

//    根据id查询
    @Override
    public Result findById(String id) {
        CropsDisease cropsDisease = cropsDiseaseMapper.selectByPrimaryKey(id);
        return new Result(cropsDisease != null?true:false,
                cropsDisease != null? StatusCode.FIND_Ok:StatusCode.FIND_ERROR,
                cropsDisease != null?"查询成功":"查询失败",
                cropsDisease);
    }


    /**
     *  分页条件查询
     * @param page  当前页
     * @param size  每页大小
     * @param cropsDiseaseParam 查询条件
     * @return
     */
    @Override
    public PageInfo findByPage(Integer page,
                               Integer size,
                               CropsParam cropsDiseaseParam) {
//        开启分页
        PageHelper.startPage(page,size);
        List<CropsDisease> list = cropsDiseaseMapper.findByPage(cropsDiseaseParam);
        return new PageInfo(list);
    }

    /**
     *  根据id删除病虫害数据
     * @param id  病虫害id
     * @return
     */
    @Override
    public Integer deleteById(Integer id) {
//        移除中间表中的数据
        cropsInfoDiseaseMapper.deleteByDid(id);

//        删除病虫害数据
        return cropsDiseaseMapper.deleteByPrimaryKey(id);
    }


    /**
     * 根据农作物id查询病虫害
     * @param id
     * @return
     */
    @Override
    public List<CropsDisease> findByCropsId(Long id) {
        return cropsDiseaseMapper.findByCropsId(id);
    }

    /**
     * 根据病虫害id修改数据
     * @param cropsDisease
     * @return
     */
    @Override
    public Integer updateById(CropsDisease cropsDisease) {
        return cropsDiseaseMapper.updateByPrimaryKeySelective(cropsDisease);
    }

    /**
     * 添加病虫害信息同时绑定农作物
     * @param cropsDisease 病虫害数据
     * @param cropsId 农作物id
     * @param stage
     */
    @Override
    public void add(CropsDisease cropsDisease, Integer cropsId, String stage) {
        cropsDiseaseMapper.insertSelective(cropsDisease);

        Long id = cropsDisease.getId();
//        创建病虫害 封装数据
        CropsInfoDisease cropsInfoDisease = new CropsInfoDisease();
        cropsInfoDisease.setCid(Long.parseLong(cropsId.toString()));
        cropsInfoDisease.setDid(id);
        cropsInfoDisease.setStage(stage);

//        将数据添加到中间表中
        cropsInfoDiseaseMapper.insertSelective(cropsInfoDisease);

    }
}
